package com.base.cn.platform.os.controller.web.op.coupon;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.op.coupon.OpCouponShop;
import com.base.cn.platform.os.service.manage.op.coupon.OpCouponCodeService;
import com.base.cn.platform.os.service.manage.op.coupon.OpCouponService;
import com.base.cn.platform.os.service.manage.op.coupon.OpCouponShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 个人中心的优惠券
 * @author ll
 * @date 2018/5/30 14:04
 */
@Controller
@RequestMapping("/web/uc/op")
public class WebOpCouponController extends WebBaseController {

    @Autowired
    private OpCouponCodeService opCouponCodeService;
    @Autowired
    private OpCouponShopService opCouponShopService;
    @Autowired
    private OpCouponService opCouponService;

    /**
     * 我的优惠券
     * @param request
     * @return
     */
    @RequestMapping("/myCoupon")
    public String myCoupon(HttpServletRequest request, Model model) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String,Object> parmas = new HashMap<>();
        parmas.put("userId",userId);
        parmas.put("source",1);
        List<Map<String,Object>> content=opCouponCodeService.queryUserCouponCodeList(parmas,false,false);
        parmas.put("status",5);
        List<Map<String,Object>> useNum=opCouponCodeService.queryUserCouponCodeList(parmas,false,false);
        parmas.put("status",6);
        parmas.put("endTime",new Date());
        List<Map<String,Object>> expirationNum=opCouponCodeService.queryUserCouponCodeList(parmas,false,false);
        model.addAttribute("content", content.size());
        model.addAttribute("expirationNum", expirationNum.size());
        model.addAttribute("useNum", useNum.size());
        return resultPage("/op/myAccount/u-myCoupon");
    }
    /**
     * 我的优惠券
     * @param request
     * @return
     */
    @RequestMapping("/ajax/myAjaxCoupon")
    public String myAjaxCoupon(HttpServletRequest request,Model model,
                               @RequestParam(value = "currentPage",required = false,defaultValue = "1")Integer currentPage,
                               @RequestBody Map<String,Object> params,
                               @RequestParam(value = "order",required = false)Integer order){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        params.put("userId",userId);
        Map<String,Object> couponCodeListPage = opCouponCodeService.queryOpCouponCodeListPage(params, currentPage, true, true);
        model.addAttribute("dataMap",couponCodeListPage);
        return resultPage("/op/myAccount/u-ajax-myCoupon");
    }

    /**
     * 获取优惠券关联的商品数据
     * @param couponId 优惠券ID
     * @return Map<String,Object>
     */
    @RequestMapping("/showCouponShopData")
    @ResponseBody
    public Map<String,Object> showCouponShopData(@RequestParam("couponId") BigDecimal couponId){
        List<OpCouponShop> couponShopList = opCouponShopService.showCouponShopData(couponId);
        return ResultUtil.SUCCESS(couponShopList);
    }

    /**
     * 用户领取优惠券
     * @param request HttpServletRequest
     * @param couponIds 优惠券ID串
     * @return Map<String,Object>
     */
    @RequestMapping("/couponReceive")
    @ResponseBody
    public Map<String,Object> couponReceive(HttpServletRequest request,
                                            @RequestParam("couponIds") String couponIds){
        //获取用户ID
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return opCouponService.couponReceive(couponIds,userId);
    }

    /**
     * 个人中心可以领取的优惠券数据列表
     * @param request HttpServletRequest
     * @return Map<String,Object>
     */
    @RequestMapping("/findForUsersCouponList")
    @ResponseBody
    public Map<String,Object> findForUsersCouponList(HttpServletRequest request){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return opCouponService.findForUsersCouponList(userId);
    }
}
